package com.ibm.commerce.url.tasks.orgadminconsole;

/**
 * Info 1
 * Info 2
 * Info 3
 * Info 4
 * Info 5
 * Info 6
 */

import com.ibm.commerce.url.harness.*;
import com.ibm.commerce.url.util.ContentSearcher;
import com.ibm.commerce.url.util.Log;
import com.ibm.commerce.url.exception.*;
import com.ibm.wc.test.exception.DatablockMissingException;

import java.util.LinkedHashMap;
import java.util.Map;

public class CreateNewUser extends URLTestTask {
	
	public static final String INPUT_PARAMETER_NAME_LOGONID = "logonId";
	public static final String INPUT_PARAMETER_NAME_TITLE = "title";
	public static final String INPUT_PARAMETER_NAME_FIRSTNAME = "firstName";
	public static final String INPUT_PARAMETER_NAME_MIDDLENAME = "middleName";
	public static final String INPUT_PARAMETER_NAME_LASTNAME = "lastName";
	public static final String INPUT_PARAMETER_NAME_PASSWORD = "password";
	public static final String INPUT_PARAMETER_NAME_CHALLENGEQUESTION = "challengeQuestion";
	public static final String INPUT_PARAMETER_NAME_CHALLEANGEANSWER = "challengeAnswer";
	public static final String INPUT_PARAMETER_NAME_SHOPPERPOLICYID = "shopperPolicyId";
	public static final String INPUT_PARAMETER_NAME_ACCOUNTSTATUS = "accountStatus";
	public static final String INPUT_PARAMETER_NAME_PASSWORDEXPIRED = "passwordExpired";
	public static final String INPUT_PARAMETER_NAME_EMPLOYEEID = "employeeId";
	public static final String INPUT_PARAMETER_NAME_EMPLOYEETYPE = "employeeType";
	public static final String INPUT_PARAMETER_NAME_DEPTNUMBER = "deptNumber";
	public static final String INPUT_PARAMETER_NAME_MANAGERNAME = "managerName";
	public static final String INPUT_PARAMETER_NAME_SECRETARYNAME = "secretaryName";
	public static final String INPUT_PARAMETER_NAME_LANGUAGE = "language";
	public static final String INPUT_PARAMETER_NAME_ADDRESS1 = "address1";
	public static final String INPUT_PARAMETER_NAME_ADDRESS2 = "address2";
	public static final String INPUT_PARAMETER_NAME_ADDRESS3 = "address3";
	public static final String INPUT_PARAMETER_NAME_CITY = "city";
	public static final String INPUT_PARAMETER_NAME_STATE = "state";
	public static final String INPUT_PARAMETER_NAME_ZIP = "zip";
	public static final String INPUT_PARAMETER_NAME_COUNTRY = "country";
	public static final String INPUT_PARAMETER_NAME_EMAIL1 = "email1";
	public static final String INPUT_PARAMETER_NAME_EMAIL2 = "email2";
	public static final String INPUT_PARAMETER_NAME_PHONE1 = "phone1";
	public static final String INPUT_PARAMETER_NAME_PHONE2 = "phone2";
	public static final String INPUT_PARAMETER_NAME_FAX1 = "fax1";
	public static final String INPUT_PARAMETER_NAME_FAX2 = "fax2";
	public static final String INPUT_PARAMETER_BESTTIEMID = "bestTimeId";
	public static final String INPUT_PARAMETER_PARENTORG = "parentOrg";
	
	public static final String OUTPUT_PARAMETER_USERID = "userId";
	
	private static CreateNewUser instance = new CreateNewUser();
	private CreateNewUser(){}
	
	public static void execute() throws TaskFailException, DatablockMissingException {
		instance.runTask();
	}
	public static void execute(int counterIndex) throws TaskFailException, DatablockMissingException {
		instance.runTask(counterIndex);
	}
	
	protected void executeTask() throws TaskFailException {
		createNewUserStep1();
		createNewUserStep2();
		createNewUserStep3();
	}

	private static Map<String, String> CreateNewUser_BUYC001 = new LinkedHashMap<String, String>();
	static{
		CreateNewUser_BUYC001.put("XMLFile","buyerconsole.BuyCreateUserDialog");
	}
	private void createNewUserStep1() throws TaskFailException {
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering createNewUserStep1");
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/BuyCreateUserView", WEB_FORM_GET, CreateNewUser_BUYC001);
		
		putParameterInReusableMap("adminId",strSearchReg("&adminId=\\\" \\+ (.*)", ContentSearcher.FLAG_EXTRACT_FROM_GROUP));
		
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting createNewUserStep1");
	}
	
	private static Map<String, String> CreateNewUser_OASE002 = new LinkedHashMap<String, String>();
	static{
		CreateNewUser_OASE002.put("taskName","ManageExcludingAD");
		CreateNewUser_OASE002.put("searchType","0");
		CreateNewUser_OASE002.put("adminId","");
		CreateNewUser_OASE002.put("searchString","");
		CreateNewUser_OASE002.put("returningUserCriteria","1");
		CreateNewUser_OASE002.put("maxThreshold","100");
		CreateNewUser_OASE002.put("firstLoad","1");
	}
	private void createNewUserStep2() throws TaskFailException{
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering createNewUserStep2");
		
		putParameterInMap(CreateNewUser_OASE002, "adminId", getParameterFromReusableMap("adminId"));
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/OASearchForOrgsICanAdminView", WEB_FORM_GET, CreateNewUser_OASE002);
		
	//	String parentOrg = getParameterFromDataFile(INPUT_PARAMETER_PARENTORG);
		String temp1 = strSearchReg("resultNames\\[(.*)\\] =", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
		String orgId = strSearchReg("resultIDs\\["+temp1+"\\] = \\'(.*)\\';", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
		String quickTextEntry = strSearchReg("resultNames\\["+temp1+"\\] = \\'(.*)\\';", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
		putParameterInReusableMap("orgId",orgId);		
		putParameterInReusableMap("quickTextEntry",quickTextEntry);
				
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting createNewUserStep2");	
	}
	
	private static Map<String, String> CreateNewUser_USER004 = new LinkedHashMap<String, String>();
	static{
		CreateNewUser_USER004.put("encoding","UTF-8");
		CreateNewUser_USER004.put("XML","");
	}
	private static Map<String, String> CreateNewUser_OASE005 = new LinkedHashMap<String, String>();
	static{
		CreateNewUser_OASE005.put("taskName","ManageExcludingAD");
		CreateNewUser_OASE005.put("searchType","0");
		CreateNewUser_OASE005.put("adminId","");
		CreateNewUser_OASE005.put("searchString","");
		CreateNewUser_OASE005.put("returningUserCriteria","1");
		CreateNewUser_OASE005.put("maxThreshold","100");
		CreateNewUser_OASE005.put("firstLoad","1");
	}
	private static Map<String, String> CreateNewUser_NEWD006 = new LinkedHashMap<String, String>();
	static{
		CreateNewUser_NEWD006.put("ActionXMLFile","buyerconsole.BuyUserAdminList");
		CreateNewUser_NEWD006.put("cmd","BuyAdminConUserAdminView");
		CreateNewUser_NEWD006.put("userLogonId","");
		CreateNewUser_NEWD006.put("userLogonIdSearchType","1");
	}
	private static Map<String, String> CreateNewUser_BUYA008 = new LinkedHashMap<String, String>();
	static{
		CreateNewUser_BUYA008.put("encoding","UTF-8");
		CreateNewUser_BUYA008.put("ActionXMLFile","buyerconsole.BuyUserAdminList");
		CreateNewUser_BUYA008.put("listsize","12");
		CreateNewUser_BUYA008.put("resultssize","0");
		CreateNewUser_BUYA008.put("startindex","0");
		CreateNewUser_BUYA008.put("userLogonId","");
		CreateNewUser_BUYA008.put("viewTaskName","NewDynamicListView");
		CreateNewUser_BUYA008.put("userLogonIdSearchType","1");
		CreateNewUser_BUYA008.put("storeDir","no");
	}
	private void createNewUserStep3() throws TaskFailException{	
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering createNewUserStep3");
		
		String CreateNewUser_logonId_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_LOGONID) + System.currentTimeMillis();
		String CreateNewUser_title_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_TITLE);
		String CreateNewUser_firstName_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_FIRSTNAME);
		String CreateNewUser_middleName_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_MIDDLENAME);
		String CreateNewUser_lastName_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_LASTNAME);
		String CreateNewUser_password_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_PASSWORD);
		String CreateNewUser_challengeQuestion_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_CHALLENGEQUESTION);
		String CreateNewUser_challengeAnswer_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_CHALLEANGEANSWER);
		String CreateNewUser_shopperPolicyId_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_SHOPPERPOLICYID);
		String CreateNewUser_accountStatus_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_ACCOUNTSTATUS);
		String passwordExpired = getParameterFromDataFile(INPUT_PARAMETER_NAME_PASSWORDEXPIRED)==null?"0":getParameterFromDataFile(INPUT_PARAMETER_NAME_PASSWORDEXPIRED);
		String CreateNewUser_employeeId_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_EMPLOYEEID);
		String CreateNewUser_employeeType_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_EMPLOYEETYPE);
		String CreateNewUser_deptNumber_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_DEPTNUMBER);
		String CreateNewUser_managerName_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_MANAGERNAME);
		String CreateNewUser_secretaryName_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_SECRETARYNAME);
		String CreateNewUser_language_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_LANGUAGE);
		String QuickTextEntry = getParameterFromReusableMap("quickTextEntry");
		String CreateNewUser_address1_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_ADDRESS1);
		String CreateNewUser_address2_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_ADDRESS2);
		String CreateNewUser_address3_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_ADDRESS3);
		String CreateNewUser_city_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_CITY);
		String CreateNewUser_state_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_STATE);
		String CreateNewUser_zip_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_ZIP);
		String CreateNewUser_country_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_COUNTRY);
		String CreateNewUser_email1_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_EMAIL1);
		String CreateNewUser_email2_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_EMAIL2);
		String CreateNewUser_phone1_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_PHONE1);
		String CreateNewUser_phone2_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_PHONE2);
		String CreateNewUser_fax1_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_FAX1);
		String CreateNewUser_fax2_in = getParameterFromDataFile(INPUT_PARAMETER_NAME_FAX2);
		String CreateNewUser_bestTimeId_in = getParameterFromDataFile(INPUT_PARAMETER_BESTTIEMID);
		String orgId = getParameterFromReusableMap("orgId");		
		
		Log.info("Create New User: "+CreateNewUser_logonId_in);		
		
		String UserWizardXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
            "<XML>\r\n" +
            " <redirecturl>UniversalDialogView</redirecturl>\r\n" +
            " <XMLFile>buyerconsole.BuyCreateUserDialog</XMLFile>\r\n" +
            " <logonId>"+CreateNewUser_logonId_in+"</logonId>\r\n" +
            " <personTitle>"+CreateNewUser_title_in+"</personTitle>\r\n" +
            " <firstName>"+CreateNewUser_firstName_in+"</firstName>\r\n" +
            " <middleName>"+CreateNewUser_middleName_in+"</middleName>\r\n" +
            " <lastName>"+CreateNewUser_lastName_in+"</lastName>\r\n" +
            " <logonPassword>"+CreateNewUser_password_in+"</logonPassword>\r\n" +
            " <logonPasswordVerify>"+CreateNewUser_password_in+"</logonPasswordVerify>\r\n" +
            " <challengeQuestion>"+CreateNewUser_challengeQuestion_in+"</challengeQuestion>\r\n" +
            " <challengeAnswer>"+CreateNewUser_challengeAnswer_in+"</challengeAnswer>\r\n" +
            " <policyAccountId>"+CreateNewUser_shopperPolicyId_in+"</policyAccountId>\r\n" +
            " <userStatus>"+CreateNewUser_accountStatus_in+"</userStatus>\r\n" +
            " <profileType>B</profileType>\r\n" +
            " <passwordExpired>"+passwordExpired+"</passwordExpired>\r\n" +
            " <employeeId>"+CreateNewUser_employeeId_in+"</employeeId>\r\n" +
            " <employeeType>"+CreateNewUser_employeeType_in+"</employeeType>\r\n" +
            " <departmentNumber>"+CreateNewUser_deptNumber_in+"</departmentNumber>\r\n" +
            " <manager>"+CreateNewUser_managerName_in+"</manager>\r\n" +
            " <secretary>"+CreateNewUser_secretaryName_in+"</secretary>\r\n" +
            " <preferredLanguage>"+CreateNewUser_language_in+"</preferredLanguage>\r\n" +
            " <LUS_QuickTextEntry>"+QuickTextEntry+"</LUS_QuickTextEntry>\r\n" +
            " <LUS_SearchTextField></LUS_SearchTextField>\r\n" +
            " <LUS_CriteriaDropDown>1</LUS_CriteriaDropDown>\r\n" +
            " <address1>"+CreateNewUser_address1_in+"</address1>\r\n" +
            " <address2>"+CreateNewUser_address2_in+"</address2>\r\n" +
            " <address3>"+CreateNewUser_address3_in+"</address3>\r\n" +
            " <city>"+CreateNewUser_city_in+"</city>\r\n" +
            " <state>"+CreateNewUser_state_in+"</state>\r\n" +
            " <zipCode>"+CreateNewUser_zip_in+"</zipCode>\r\n" +
            " <country>"+CreateNewUser_country_in+"</country>\r\n" +
            " <email1>"+CreateNewUser_email1_in+"</email1>\r\n" +
            " <email2>"+CreateNewUser_email2_in+"</email2>\r\n" +
            " <phone1>"+CreateNewUser_phone1_in+"</phone1>\r\n" +
            " <phone2>"+CreateNewUser_phone2_in+"</phone2>\r\n" +
            " <fax1>"+CreateNewUser_fax1_in+"</fax1>\r\n" +
            " <fax2>"+CreateNewUser_fax2_in+"</fax2>\r\n" +
            " <bestCallingTime>"+CreateNewUser_bestTimeId_in+"</bestCallingTime>\r\n" +
            " <parentMemberId>"+orgId+"</parentMemberId>\r\n" +
            "</XML>\r\n";		
		
		putParameterInMap(CreateNewUser_USER004,"XML",UserWizardXML);
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/UserRegistrationAdminAdd", WEB_FORM_POST, CreateNewUser_USER004);
		  
		putParameterInMap(CreateNewUser_OASE005,"adminId",getParameterFromReusableMap("adminId"));
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/OASearchForOrgsICanAdminView", WEB_FORM_GET, CreateNewUser_OASE005);
			
		putParameterInMap(CreateNewUser_NEWD006,"userLogonId",CreateNewUser_logonId_in); 
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/NewDynamicListView", WEB_FORM_GET, CreateNewUser_NEWD006);
			
		putParameterInMap(CreateNewUser_BUYA008,"userLogonId",CreateNewUser_logonId_in);
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/BuyAdminConUserAdminView", WEB_FORM_POST, CreateNewUser_BUYA008);
		  
		String newUserId = strSearchReg("javascript:changeUserAdmin\\(\\\\'(.*)\\\\'\\);", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
		
		putParameterInOutputMap(OUTPUT_PARAMETER_USERID,newUserId);
		
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting createNewUserStep3");
	}
	
}
